﻿using System;
using System.Collections.Generic;
using System.Linq;
using Abp.Runtime.Session;
using pandx.Mulan.Authorization.Users.Dto;
using pandx.Mulan.DataImExPorting.Excel;
using pandx.Mulan.Dto;
using pandx.Mulan.Storage;

namespace pandx.Mulan.Authorization.Users.Exporting
{
    public class UserListExcelExporter:NpoiExcelExporterBase,IUserListExcelExporter
    {
        private readonly IAbpSession _abpSession;

        public UserListExcelExporter(IAbpSession abpSession, ITempFileCacheManager tempFileCacheManager) : base(
            tempFileCacheManager)
        {
            _abpSession = abpSession;
        }

        public FileDto ExportToExcel(List<UserListDto> userListDtos)
        {
            return CreateExcelPackage("用户列表_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ".xlsx",
                excelPackage =>
                {
                    var sheet = excelPackage.CreateSheet("用户列表");
                    AddHeader(sheet,
                        "所属单位",
                        "用户名",
                        "姓名",
                        "性别",       
                        "角色",          
                        "激活",
                        "锁定",
                        "Email地址",
                        "电话号码",
                        "创建时间"
                    );
                    AddObjects(sheet,2,userListDtos,
                        _=>string.Join(",",_.OrganizationUnits.Select(o=>o.DisplayName)),
                        _=>_.UserName,
                        _=>_.Name,
                        _=>_.Gender==Gender.Male?"男":"女",
                        _=>string.Join(",",_.Roles.Select(r=>r.RoleName)),
                        _=>_.IsActive?"是":"否",
                        _=>_.OnlyLockedUsers?"是":"否",
                        _=>_.EmailAddress,
                        _=>_.PhoneNumber,
                        _=>_.CreationTime
                    );
                    for(var i=1;i<=userListDtos.Count;i++)
                    {
                        SetCellDataFormat(sheet.GetRow(i).Cells[9],"yyyy-MM-dd HH:mm:ss");
                    }
                    

                    for (var i = 0; i < 10; i++)
                    {
                   
                        sheet.AutoSizeColumn(i);
                    }
                });
        }
    }
}
